/* api */
import { addDingtalkAssistant, addDingtalkAssistantDoc, getDingtalkAssistantDoc } from "@src/api/AIApi"
/* components */
import { SettingGPTViewDialog } from "@gpt/components"
import ProtocolDialog from "@gpt/views/edit/dingtalk-assistant/protocol-dialog"
import DocDialog from "@gpt/views/edit/dingtalk-assistant/doc-dialog"
/* enum */
import ComponentNameEnum from "@model/enum/ComponentNameEnum"
/* model */
import MsgModel from "@model/MsgModel"
/* vue */
import { defineComponent, PropType } from "vue"
/* scss */
import '@gpt/views/home/url-description-dialog.scss'
/* util */
import {
  DingtalkOpenAuthBaseParams, 
  DingtalkOpenAuthTypeEnum, 
  DingtalkOpenAuthStatusEnum,
  getCorpId,
  getAppId,
  getRootWindow
} from "pub-bbx-utils"
import { message } from "@src/util/message"
import { AddDingtalkAssistantDocModel, AddDingtalkAssistantModel } from "@model/param/in/AI"
import { isFalsy } from "@src/util/type"
import { getDDFreeLoginConfig } from "@src/api/UserCenterApi"
import { Loading } from "element-ui"
import { openDingtalkAuth } from "@src/util/dd/auth"
import { DingtalkAuthTypeItem } from "@model/dingtalk"

type SettingGPTViewDialogComponentType = InstanceType<typeof SettingGPTViewDialog>
type ProtocolDialogComponentType = InstanceType<typeof ProtocolDialog>
type DocDialogComponentType = InstanceType<typeof DocDialog>

enum SettingGPTDingtalkAssistantSettingDialogEventNameEnum {
  Confirm = 'confirm',
  Loading = 'loading',
  Change = 'change'
}

export default defineComponent({
  name: ComponentNameEnum.SettingGPTDingtalkAssistantSettingDialog,
  props: {
    onInput: {
      type: Function
    },
    onConfirm: {
      type: Function
    },
    onLoading: {
      type: Function
    },
    onChange: {
      type: Function
    }
  },
  data() {
    return {
      loading: false,
      loadingInstance: null as any
    }
  },
  computed: {
    protocolDialogComponent(): ProtocolDialogComponentType {
      return this.$refs.ProtocolDialog as ProtocolDialogComponentType
    },
    docDialogComponent(): DocDialogComponentType {
      return this.$refs.DocDialog as DocDialogComponentType
    }
  },
  methods: {
    showLoading() {
      this.loadingInstance = Loading.service({
        customClass: 'setting-gpt-dingtalk-assistant-setting-dialog-loading',
        lock: true,
        fullscreen: true,
      })
      this.$emit(SettingGPTDingtalkAssistantSettingDialogEventNameEnum.Loading, true)
    },
    closeLoading() {
      this.$emit(SettingGPTDingtalkAssistantSettingDialogEventNameEnum.Loading, false)
      this.loadingInstance && this.loadingInstance.close()
    },
    openDialog() {
      this.protocolDialogComponent.openDialog()
    },
    closeDialog() {
      this.protocolDialogComponent.closeDialog()
    },
    openDocDialog(docUrl?: string) {
      this.docDialogComponent.open(docUrl)
    },
    async editDocDialog() {
      try {
        
        const docResult = await getDingtalkAssistantDoc()
        const isFail = MsgModel.isFail(docResult)
        if (isFail) {
          message.error(docResult.message)
          return
        }
        
        const docUrl = docResult.data || ''
        
        this.openDocDialog(docUrl)
        
      } catch (error) {
        console.error('editDocDialog error', error)
      }
    },
    closeDocDialog() {
      this.docDialogComponent.close()
    },
    closeAllDialog() {
      this.closeDialog()
      this.closeDocDialog()
    },
    onProtocolDialogConfirm() {
      console.log('onProtocolDialogConfirm')
      this.openDingtalkAuth()
    },
    async openDingtalkAuth() {
      
      console.log('openDingtalkAuth')
      
      const params: DingtalkOpenAuthBaseParams = {
        rpcScope: 'Assistant.Management.Write',
        type: DingtalkOpenAuthTypeEnum.Personal
      }
      
      console.log('openDingtalkAuth params: ', params)
      
      await openDingtalkAuth(params as DingtalkAuthTypeItem)
      
      const authCode = ''
      this.addDingtalkAssistantHandler(authCode)

    },
    async addDingtalkAssistantHandler(authCode: string) {
      
      console.log('addDingtalkAssistantHandler', authCode)
      
      const params: AddDingtalkAssistantModel = {
        assistantId: '',
        authCode
      }
      const result = await addDingtalkAssistant(params)
      const isFail = MsgModel.isFail(result)
      if (isFail) {
        message.error(result.message)
        return
      }
      
      this.openDocDialog()
    },
    onDocDialogConfirm(value: string) {
      try {
        console.log('onDocDialogConfirm', value)
        this.addDingtalkAssistantDocHandler(value)
      } catch (error) {
        console.error('onDocDialogConfirm error', error)
        this.closeLoading()
      }
    },
    async addDingtalkAssistantDocHandler(docUrl: string) {
      
      console.log('addDingtalkAssistantDocHandler', docUrl)
      
      this.showLoading()
      
      const dingtalkAuthParams: DingtalkOpenAuthBaseParams = {
        rpcScope: 'Assistant.Knowledge.Write',
        type: DingtalkOpenAuthTypeEnum.Personal
      }
      
      console.log('openDingtalkAuth dingtalkAuthParams: ', dingtalkAuthParams)
      
      await openDingtalkAuth(dingtalkAuthParams as DingtalkAuthTypeItem)
      
      const authCode = ''
      const params: AddDingtalkAssistantDocModel = {
        assistantId: '',
        authCode,
        docUrl
      }
      
      console.log('addDingtalkAssistantDocHandler', params)
      
      const result = await addDingtalkAssistantDoc(params)
      
      console.log('addDingtalkAssistantDocHandler', result)
      
      if (MsgModel.isFail(result)) {
        message.error(result.message)
        this.closeLoading()
        return
      }

      this.closeLoading()
      
      this.closeAllDialog()
      
      this.emitConfirm()
      
    },
    emitConfirm() {
      this.$emit(SettingGPTDingtalkAssistantSettingDialogEventNameEnum.Confirm)
    },
    emitChange() {
      this.$emit(SettingGPTDingtalkAssistantSettingDialogEventNameEnum.Change)
    }
  },
  render() {
    return (
      <div class={ComponentNameEnum.SettingGPTDingtalkAssistantSettingDialog}>
        <ProtocolDialog
          ref="ProtocolDialog"
          onConfirm={this.onProtocolDialogConfirm}
        >
        </ProtocolDialog>
        <DocDialog
          ref="DocDialog"
          onConfirm={this.onDocDialogConfirm}
        >
        </DocDialog>
      </div>
    )
  }
})